﻿using Client.Processes;
using Client.ViewModels;
using Client.Views.WebImport;

namespace Client.Commands
{
    internal class ImportWebInfosCommand : BaseCommand
    {
        private ReadMovies readMovies;

        public ImportWebInfosCommand()
        {
            this.readMovies = new ReadMovies();
            this.readMovies.OnCompleted += m =>
            {
                AllMoviesViewModel.Default.IsBusy = false;
                AllMoviesViewModel.Default.MovieItems = m;
            };

        }

        public override bool CanExecute(object parameter)
        {
            var viewModel = (parameter as WorkspaceViewModel);
            return viewModel != null && !MainViewModel.Default.ActiveWorkspace.IsBusy;
        }

        public override void Execute(object parameter)
        {
            var viewModel = (parameter as WorkspaceViewModel);

            var wizard = new WebImportWizardView();
            var result = wizard.ShowDialog(viewModel.SelectedItems);

            //Asynchrones Refresh der All Movies Liste, bei Moviedetails kein asynchrones Refresh
            if ((bool)result)
            {
                AllMoviesViewModel.Default.IsBusy = true;
                readMovies.Process();
            }
        }
    }
}
